/******************************************************数字*****************************************************/
_$.Number=function (target){
    var _this = this;
    _$.Number.superclass.constructor.call(_this,target);
};
_$.regClass("Number",_$.Number);

_$.extend(_$.Number,_$.FormItem, {
    _cls:_$._clsPre + "Number",
    _uiCls:_$._uiPreCls + "Number",
    _editModel:function(){
        var _this = this;
        _this._clearShow();
        var source='<div class="Number"><span class="sub_num" >-</span><input type="text" class="num_inp" /><span class="add_num" >+</span></div>';
        _this._replaceNode(source);
        _this.component = $(".num_inp",_this.el);
        _this.subBtn = $(".sub_num",_this.el);
        _this.addBtn = $(".add_num",_this.el);
        _this.el.addClass(_this._uiCls);
        _this._setStyle();
        _this.setOption("model",_EDITABLE_);
        _this._init();
        _this._bindEvents();
    },
    _clearEdit:function(){
        var _this = this;
        if( _this.addBtn){
            _this.addBtn.unbind();
            _this.addBtn.remove();
        }
        if(_this.subBtn){
            _this.subBtn.unbind();
            _this.subBtn.remove();
        }
        if(_this.component){
            _this.component.unbind();
            _this.component.remove();
        }
    },
    _attrProps:function(){
        var _this = this;
        var properties = _$.Number.superclass._attrProps.call(_this);
        properties.addAll(["onblur","onchange",{"numberType":"number","minNum":"number","maxNum":"number"}]);
        return properties;
    },
    _onblur:function(){
        var _this = this;
        _this._on("onblur", function(e){
            var value= e.value ;
            var numberType = _this.getOption("numberType");

            var reg;
            if(numberType == 0){
                reg = /^\d+$/;
            }else if(numberType == 1){
                reg = /^-?\d+$/;
            }else if(numberType == 2){
                reg = /^[1-9]\d*$/;
            }else if(numberType == 3){
                reg = /^-?\d+$/;
            }

            if(!reg.test(value)){
                _this.component.val(0);
            }
        });
    },
    _changNum: function(func){
        var _this = this;
        var val = _this.getValue();
        var num = parseInt(val);
        num = func.call(null,num);
        var numberType = _this.getOption("numberType");
        var minNum = _this.getOption("minNum");
        var maxNum = _this.getOption("maxNum");

        var reg;
        if(numberType == 0){
            reg = /^\d+$/;
        }else if(numberType == 1){
            reg = /^-?\d+$/;
        }else if(numberType == 2){
            reg = /^[1-9]\d*$/;
        }else if(numberType == 3){
            reg = /^-?\d+$/;
        }


        if(!reg.test(num+"") || (isNotEmpty(minNum) && num< minNum)  || (isNotEmpty(maxNum) && num > maxNum) ){
            _this.component.val(val);
        }else{
            _this.component.val(num);
        }
    },
    _onSubBtnClick:function(){
        var _this = this;
        _this.subBtn.bind("click",function(e){
            _this._changNum(function(num){
                num--;
                return num;
            });
        });
    },
    _onAddBtnClick:function(){
        var _this = this;
        _this.addBtn.bind("click",function(e){
            _this._changNum(function(num){
                num++;
                return num;
            });
        });
    },
    _bindEvents:function(){
        var _this = this;
        _$.Text.superclass._getEvents.call(_this, ["onblur","onchange"]);

        if(_this._isEdit()){
           _this._onblur();
            _this._onSubBtnClick();
            _this._onAddBtnClick();
            var event = {
                "name":_this.id
            };

            _this.component.bind("blur",function(){
                event['source'] = _this.component;
                event['value'] = _this.getValue();
                _this._blurHandler(event);
            });
        }
    },
    _blurHandler:function(event){
        var _this = this;
        _this._fire("onblur",event);
        var original = isNotEmpty(_this.original)?_this.original:_this.getOption("initValue");
        var value = _this.getValue();
        if(original != value){
            _this.original=value;
            _this._fire("onchange",event);
        }
    }
});
